/**
 * @file wa_img.c
 *
 */

 /*********************
 *      INCLUDES
 *********************/
#include "wa_conf.h"
#if USE_WA_IMG != 0
#include "wa_img.h"

 /*********************
 *      DEFINES
 *********************/

 /**********************
 *      TYPEDEFS
 **********************/

 /**********************
 *  STATIC PROTOTYPES
 **********************/
static wa_obj_callback ancestor_callback;
static void wa_img_callback(wa_sign* sign);
/**********************
 *  STATIC VARIABLES
 **********************/


 /**********************
 *      MACROS
 **********************/

 /**********************
 *   GLOBAL FUNCTIONS
 **********************/
 /**
 * img module init
 */
void wa_img_module_init(void)
{
}

/**
* create a img
* @param par
* @return img obj
*/
wa_obj* wa_img_create(wa_obj* par)
{
	wa_obj* new_img;
	wa_img_ext* img_ext;

	new_img = wa_obj_create(par, NULL);
	if (new_img == NULL)
	{
		return new_img;
	}

	if (ancestor_callback == NULL)
	{
		ancestor_callback = wa_obj_get_callback(new_img);
	}
	img_ext = wa_obj_malloc_ext_attr(new_img, sizeof(wa_img_ext));
	if (img_ext == NULL)
	{
		return NULL;
	}

	img_ext->bitmap = NULL;
	img_ext->img_type = WA_IMAGE_TYPE_NONE;
	img_ext->img_color = WA_COLOR_FOREGROUND;
	img_ext->back_color = WA_COLOR_BACKGROUND;
	img_ext->align_mode = WA_IMG_AL_LEFT | WA_IMG_AL_TOP;

	wa_obj_set_callback(new_img, wa_img_callback);

	return new_img;
}

/**
* img set bitmap
* @param img
* @param bitmap
*/
void wa_img_set_bitmap(wa_obj* img, wa_bitmap* bitmap)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	ext->bitmap = bitmap;
	ext->img_type = WA_IMAGE_TYPE_BITMAP;
	wa_obj_invalidate(img);
}

/**
* img set img color
* @param img
* @param img color
*/
void wa_img_set_img_color(wa_obj* img, wa_gui_color color)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	ext->img_color = color;
	wa_obj_invalidate(img);
}

/**
* img set back color
* @param img
* @param back color
*/
void wa_img_set_back_color(wa_obj* img, wa_gui_color color)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	ext->back_color = color;
	wa_obj_invalidate(img);
}

/**
* img set align mode
* @param img
* @param align mode
*/
void wa_img_set_align_mode(wa_obj* img, uint8_t align_mode)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	ext->align_mode = align_mode;
	wa_obj_invalidate(img);
}

/**
* img get bitmap
* @param img
* @return bitmap
*/
wa_bitmap* wa_img_get_bitmap(wa_obj* img)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	return ext->bitmap;
}

/**
* img get img color
* @param img
* @return img color
*/
wa_gui_color wa_img_get_img_color(wa_obj* img)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	return ext->img_color;
}

/**
* img get back color
* @param img
* @return back color
*/
wa_gui_color wa_img_get_back_color(wa_obj* img)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	return ext->back_color;
}

/**
* img get align mode
* @param img
* @return align mode
*/
uint8_t wa_img_get_align_mode(wa_obj* img)
{
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(img);
	return ext->align_mode;
}

/**********************
*   STATIC FUNCTIONS
**********************/
/**
 * draw bitmap
 * @param obj
 */
static void _Draw_bitmap(wa_obj* obj)
{
	int img_x = 0;
	int img_y = 0;
	int img_w, img_h;
	wa_img_ext* ext;
	ext = wa_obj_get_ext_attr(obj);


	img_w = ext->bitmap->width;
	img_h = ext->bitmap->height;

	if ((ext->align_mode & 0x03) == WA_IMG_AL_LEFT)
	{
		img_x = obj->rect.x1;
	}
	else if ((ext->align_mode & 0x03) == WA_IMG_AL_RIGHT)
	{
		img_x = obj->rect.x2 - img_w;
	}
	else if ((ext->align_mode & 0x03) == WA_IMG_AL_HCENTER)
	{
		img_x = obj->rect.x1 + ((int)(wa_obj_get_width(obj)) - (int)img_w) / 2;
	}
	else
	{

	}

	if ((ext->align_mode & 0x0c) == WA_IMG_AL_TOP)
	{
		img_y = obj->rect.y1;
	}
	else if ((ext->align_mode & 0x0c) == WA_IMG_AL_BOTTOM)
	{
		img_y = obj->rect.y2 - img_h;
	}
	else if ((ext->align_mode & 0x0c) == WA_IMG_AL_VCENTER)
	{
		img_y = obj->rect.y1 + ((int)(wa_obj_get_height(obj)) - (int)img_h) / 2;
	}
	else
	{

	}

	wa_mask_draw_bitmap(img_x, img_y, ext->bitmap, &(obj->redraw_rect), ext->img_color);
}


/**
* img redraw functions
* @param obj
*/
static void _Paint(wa_obj* obj)
{
	wa_img_ext* ext;

	ext = wa_obj_get_ext_attr(obj);

	wa_mask_fillrect(obj->rect.x1, obj->rect.y1,
					wa_obj_get_width(obj),
					wa_obj_get_height(obj),
					&(obj->redraw_rect),
					ext->back_color);
	switch (ext->img_type)
	{
		case WA_IMAGE_TYPE_NONE:
		{
			break;
		}
		case WA_IMAGE_TYPE_BITMAP:
		{
			_Draw_bitmap(obj);
			break;
		}
		default:
		{
			break;
		}
	}
	
}

/**
 * img obj delete function
 * @param obj
 */
static void _Delete(wa_obj* obj)
{
}

/**
 * img obj callback function
 * @param sign
 */
static void wa_img_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;

	ancestor_callback(sign);
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_Paint(obj);
			break;
		}
		case WA_SIGNAL_DELETE:
		{
			_Delete(obj);
			break;
		}
		default:
		{
			break;
		}
	}
}

#endif
